!function () {
    var $$ = Dom7;
    $$.ajax({
        url:'http://api.nldou.com/weixinapi/init_wx_jsapi',
        method:"GET",
        data: {fromUrl:window.location.href},
        dataType:'json',
        success: function (res) {
            //console.log('初始化jssdk',res);
            if(res.code==1){
                wx.config({
                    debug: false,
                    appId: res.data.appid,
                    timestamp: res.data.timestamp,
                    nonceStr: res.data.nonceStr,
                    signature: res.data.signature,
                    jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','hideMenuItems']
                });
            }
        }
    });
    var query = window.location.search;
    var Globalqid = query.substr((query.indexOf('=') + 1));
    // var shareobj = {url: 'http://api.nldou.com/ucenter/weixinlogin?pid=1238&page=&param=qid.'+Globalqid, title: '奕斐直播|父母读库'};
    var shareobj = {url: 'http://api.nldou.com/ucenter/weixinlogin?pid=7653&page=question&param=qid.'+Globalqid, title: ''};
    var timecount,timer,Globaluid;
    var uploading = false;

    function sharecallback(sharetype)
    {
        $$.ajax({
            method: 'POST',
            url: 'http://api.nldou.com/commwapqpass/sharecallback',
            data: {qid:Globalqid,uid:Globaluid,type:sharetype},
            dataType: 'json',
            success: function (res) {return true;}
        });
    }
    function checks(s) {
        var res = {title:'非法申请',colorclass:'bg-gray',telhidden:1};
        switch (s){
            case 0:
                res.title='身份验证';
                res.colorclass='bg-green';
                res.telhidden=0;
                break;
            case 1:
                res.title='等待验证结果';
                break;
            case 3:
                res.title='审核未通过';
                break;
        }
        return res;
    }
    function checkts(ts,s) {
        var res = {title:'非法申请',colorclass:'bg-gray',telhidden:1};
        switch (ts){
            case 0:
                res.title='活动已结束';
                break;
            //活动未开始
            case 1:
                if (s==2) res.title="等待活动开始";
                else res = checks(s);
                break;
            //活动已开始
            case 2:
                if (s==2) {res.title="进入活动";res.colorclass="bg-green";}
                else res = checks(s);
                break;
        }
        return res;
    }
    function formattime(time) {
        var hour = Math.floor(time/3600).toString();
        var hour1 = hour.length>1?hour.substr(0,1):'0';
        var hour2 = hour.length>1?hour.substr(1,1):hour;
        time = time%3600;
        var min = Math.floor(time/60).toString();
        var min1 = min.length>1?min.substr(0,1):'0';
        var min2 = min.length>1?min.substr(1,1):min;
        var sec = time%60;
        sec = sec.toString();
        var sec1 = sec.length>1?sec.substr(0,1):'0';
        var sec2 = sec.length>1?sec.substr(1,1):sec;
        $$('.hour1').html(hour1);
        $$('.hour2').html(hour2);
        $$('.min1').html(min1);
        $$('.min2').html(min2);
        $$('.sec1').html(sec1);
        $$('.sec2').html(sec2);
    }
    function apply(tel) {
        $$.ajax({
            method: 'POST',
            url: 'http://api.nldou.com/commwapqpass/apply',
            data: {qid:Globalqid,uid:Globaluid,tel:tel},
            dataType: 'json',
            success: function (res) {
                //console.log('apply',res);
                uploading = false;
                if (res.code == 1) {
                    window.location.reload();
                }else{
                    alert('系统繁忙，请重试')
                }
            }
        });
    }
    $$('.act-btn').click(function () {
        if (uploading) return;
        uploading = true;
        var status = $$(this).data('status');
        var timestatus = $$(this).data('timestatus');
        var passhref = $$(this).data('passhref');
        var telhidden = $$(this).data('telhidden');

        if (status==2&&timestatus==2) {window.location.href = passhref;uploading = false;return;}
        if (status==0&&timestatus>0) {
            var telNum = $$('.tel-wrap input').val();
            if (telhidden==0) {
                if (!(/^1\d{10}$/).test(telNum)) {
                    alert('请输入正确的11位手机号');
                    uploading = false;
                    return;
                }
            }
            apply(telNum);
        }
    });
    $$.ajax({
        method: 'GET',
        url: 'http://api.nldou.com/commwapqpass/status',
        data: {qid: Globalqid},
        dataType: 'json',
        xhrFields:{"withCredentials":true},
        success: function (res) {
            //console.log('status',res);
            if (res.code == 1) {
                //封面
                var img1='<img src="'+res.data.cover.img1+'">';
                var img2='<img src="'+res.data.cover.img2+'">';
                var img3='<img src="'+res.data.cover.img3+'">';
                $$('.img-container.img1').append(img1);
                $$('.img-container.img2').append(img2);
                $$('.img-container.img3').append(img3);
                //开始计时
                timecount = parseInt(res.data.time);
                if (timecount>0){
                    timer = setInterval(function () {
                        if (timecount<0) {clearInterval(timer);window.location.reload();return;}
                        formattime(timecount);
                        timecount--;
                    },1000);
                }else{formattime(timecount);}

                //赋值
                Globaluid = parseInt(res.data.uid);
                shareobj.title = res.data.title;
                var passhref = res.data.href;
                var status = parseInt(res.data.status);
                var timestatus = parseInt(res.data.timestatus);
                var telhidden = parseInt(res.data.telhidden);
                var btn = checkts(timestatus,status);
                $$('.act-btn').html(btn.title);
                $$('.act-btn').data('status',status);
                $$('.act-btn').data('timestatus',timestatus);
                $$('.act-btn').data('passhref',passhref);
                $$('.act-btn').data('telhidden',telhidden);
                //隐藏loader
                $$('.preloader').remove();
                $$('.act-btn').addClass(btn.colorclass);
                $$('.act-btn').removeClass('hidden');
                if (btn.telhidden==0&&telhidden==0) $$('.tel-wrap').removeClass('hidden');
            }else{
                window.location.href = 'http://api.nldou.com/ucenter/weixinlogin?pid=1238&page=&param=qid.'+Globalqid;
                //console.log('http://api.nldou.com/ucenter/weixinlogin?pid=1238&page=&param=qid.'+Globalqid);
            }
        }
    });

    wx.ready(function () {
        wx.hideMenuItems({
            menuList: ['menuItem:share:qq', 'menuItem:share:weiboApp', 'menuItem:share:facebook', 'menuItem:share:QZone',
                'menuItem:copyUrl', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari']
        });
        wx.onMenuShareTimeline({
            title: shareobj.title,
            link: shareobj.url,
            imgUrl: 'http://wap.nldou.com/comm/img/comm-logo.jpg',
            success: function () {
                sharecallback('timeline');
                return true;
            }
        });
        wx.onMenuShareAppMessage({
            title: shareobj.title,
            desc: '复旦大学社会学教授沈奕斐，为您解答家庭教育的各类问题，陪您共走育儿路',
            link: shareobj.url,
            imgUrl: 'http://wap.nldou.com/comm/img/comm-logo.jpg',
            type: 'link',
            success: function () {
                sharecallback('appmsg');
                return true;
            }
        });
    });
}();
